home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-25 | 15.6 KB | 562 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: SLStrmRW.cpp
- // Release Version: $ ODF 1 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWFound.hpp"
-
- #ifndef SLSTRMRW_H
- #include "SLStrmRW.h"
- #endif
-
- #ifndef FWSTRMRW_H
- #include "FWStrmRW.h"
- #endif
-
- #ifndef FWPRIDEB_H
- #include "FWPriDeb.h"
- #endif
-
- #ifndef FWEXCLIB_H
- #include "FWExcLib.h"
- #endif
-
- #ifndef FWODFEXC_H
- #include "FWODFExc.h"
- #endif
-
- #include "SLASinks.xh"
- #include "SLObjReg.xh"
-
- #ifdef FW_BUILD_MAC
- #pragma segment FWStream
- #endif
-
- #if defined(__MWERKS__) && GENERATING68K
- // A hack to work around a bug
- #pragma import list somNewObjectInstance
- #endif
-
-
- //----------------------------------------------------------------------------------------
- // FW_NATIVE_BIGENDIAN, FW_NATIVE_LITTLEENDIAN
- // One and only one of these macros is true to define the native format
- // for storing integral values
- //----------------------------------------------------------------------------------------
- #ifdef FW_BUILD_MAC
- #define FW_NATIVE_BIGENDIAN 1
- #define FW_NATIVE_LITTLEENDIAN 0
- #endif
-
- #ifdef FW_BUILD_WIN
- #define FW_NATIVE_BIGENDIAN 0
- #define FW_NATIVE_LITTLEENDIAN 1
- #endif
-
- #if !FW_NATIVE_BIGENDIAN && !FW_NATIVE_LITTLEENDIAN
- #error "Failed to define an endianness"
- #endif
-
- //----------------------------------------------------------------------------------------
- // swapShort
- //----------------------------------------------------------------------------------------
-
- static short swapShort(short aShort)
- {
- union
- {
- short temp;
- char s[2];
- };
- temp = aShort;
- char c = s[0];
- s[0] = s[1];
- s[1] = c;
- return temp;
- }
-
- //----------------------------------------------------------------------------------------
- // swapLong
- //----------------------------------------------------------------------------------------
-
- static long swapLong(long aLong)
- {
- union
- {
- long temp;
- char s[4];
- };
- temp = aLong;
- char c = s[0];
- s[0] = s[3];
- s[3] = c;
- c = s[1];
- s[1] = s[2];
- s[2] = c;
- return temp;
- }
-
- //----------------------------------------------------------------------------------------
- // readableIdentity
- //----------------------------------------------------------------------------------------
-
- static void readableIdentity(FW_HReadableStream stream,
- void* destination,
- long count,
- long itemSize)
- {
- stream->fSink->Read(&stream->fev, destination, count*itemSize);
- }
-
- //----------------------------------------------------------------------------------------
- // writableIdentity
- //----------------------------------------------------------------------------------------
-
- static void writableIdentity(FW_HWritableStream stream,
- const void* source,
- long count,
- long itemSize)
- {
- stream->fSink->Write(&stream->fev, (void*)source, count*itemSize);
- }
-
-
- //----------------------------------------------------------------------------------------
- // readReturnError
- //----------------------------------------------------------------------------------------
-
- static FW_PlatformError readReturnError(FW_HReadableStream stream,
- void* destination,
- long count,
- long itemSize)
- {
- FW_RETURN_ERR_TRY
- {
- readableIdentity(stream, destination, count, itemSize);
- }
- FW_RETURN_ERR_CATCH
- }
-
-
- //----------------------------------------------------------------------------------------
- // writeReturnError
- //----------------------------------------------------------------------------------------
-
- static FW_PlatformError writeReturnError(FW_HWritableStream stream,
- const void* source,
- long count,
- long itemSize)
- {
- FW_RETURN_ERR_TRY
- {
- writableIdentity(stream, source, count, itemSize);
- }
- FW_RETURN_ERR_CATCH
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_PrivReadableStream_Construct
- //----------------------------------------------------------------------------------------
-
- FW_PlatformError SL_API
- FW_PrivReadableStream_Construct(FW_HReadableStream stream,
- FW_OSink* sink,
- FW_OObjectRegistry* objectRegistry,
- int numberFormat)
- {
- FW_RETURN_ERR_TRY
- {
- // Make safe for destruction
- stream->fArchiveCreatedObjectRegistry = (objectRegistry == 0);
-
- stream->fSink = sink;
- stream->fObjectRegistry = objectRegistry;
- stream->fReserved = 0;
- FW_PrimitiveSetMemory(&stream->fev, sizeof(stream->fev), 0);
-
- stream->fNumberFormat = numberFormat;
- #if FW_NATIVE_BIGENDIAN
- if (stream->fNumberFormat == FW_kStream_BigEndian)
- stream->fNumberFormat = FW_kStream_NativeEndian;
- #endif
- #if FW_NATIVE_LITTLEENDIAN
- if (stream->fNumberFormat == FW_kStream_LittleEndian)
- stream->fNumberFormat = FW_kStream_NativeEndian;
- #endif
-
- if (stream->fArchiveCreatedObjectRegistry)
- stream->fObjectRegistry = new FW_OBasicObjectRegistry;
- }
- FW_RETURN_ERR_CATCH
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_PrivReadableStream_CopyConstruct
- //----------------------------------------------------------------------------------------
-
- FW_PlatformError SL_API
- FW_PrivReadableStream_CopyConstruct(FW_HReadableStream stream,
- FW_HReadableStream original)
- {
- stream->fSink = original->fSink;
- stream->fObjectRegistry = original->fObjectRegistry;
- stream->fev = original->fev;
- stream->fNumberFormat = original->fNumberFormat;
- stream->fArchiveCreatedObjectRegistry = FALSE;
- return FW_xNoError;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivReadableStream_Destruct
- //----------------------------------------------------------------------------------------
-
- FW_PlatformError SL_API
- FW_PrivReadableStream_Destruct(FW_HReadableStream stream)
- {
- FW_RETURN_ERR_TRY
- {
- if (stream->fArchiveCreatedObjectRegistry)
- delete stream->fObjectRegistry;
- }
- FW_RETURN_ERR_CATCH
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_PrivReadableStream_ReadBytes
- //----------------------------------------------------------------------------------------
-
- FW_PlatformError SL_API
- FW_PrivReadableStream_ReadBytes(FW_HReadableStream stream,
- void* buffer,
- long count)
- {
- return readReturnError(stream, buffer, count, 1);
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_PrivReadableStream_ReadChars
- //----------------------------------------------------------------------------------------
-
- FW_PlatformError SL_API
- FW_PrivReadableStream_ReadChars(FW_HReadableStream stream,
- char* buffer,
- long count)
- {
- return readReturnError(stream, buffer, count, sizeof(buffer[0]));
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_PrivReadableStream_ReadNullTerminatedString
- //----------------------------------------------------------------------------------------
-
- FW_PlatformError SL_API
- FW_PrivReadableStream_ReadNullTerminatedString(FW_HReadableStream stream,
- char* nullTerminatedString)
- {
- Environment* const ev = &stream->fev;
- FW_OSink* const sink = stream->fSink;
-
- FW_RETURN_ERR_TRY
- {
- long availableReadBytes;
- FW_Boolean foundTerminator = FALSE;
-
- availableReadBytes = sink->GetReadableBytes(ev);
- while (!foundTerminator && availableReadBytes > 0)
- {
- sink->Read(ev, nullTerminatedString, 1);
- --availableReadBytes;
- foundTerminator = (*nullTerminatedString++ == '\0');
- }
-
- if (!foundTerminator)
- FW_Failure(FW_xReadableStream);
- }
- FW_RETURN_ERR_CATCH
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_PrivReadableStream_ReadShorts
- //----------------------------------------------------------------------------------------
-
- FW_PlatformError SL_API
- FW_PrivReadableStream_ReadShorts(FW_HReadableStream stream,
- short* buffer,
- long count)
- {
- if (stream->fNumberFormat == FW_kStream_NativeEndian)
- {
- return readReturnError(stream, buffer, count, sizeof(buffer[0]));
- }
- else
- {
- FW_PlatformError anError = readReturnError(stream, buffer, count, sizeof(buffer[0]));
-
- if (anError == FW_xNoError)
- {
- for (; count > 0; --count, ++buffer)
- *buffer = swapShort(*buffer);
- }
-
- return anError;
- }
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_PrivReadableStream_ReadLongs
- //----------------------------------------------------------------------------------------
-
- FW_PlatformError SL_API
- FW_PrivReadableStream_ReadLongs(FW_HReadableStream stream,
- long* buffer,
- long count)
- {
- if (stream->fNumberFormat == FW_kStream_NativeEndian)
- {
- return readReturnError(stream, buffer, count, sizeof(buffer[0]));
- }
- else
- {
- FW_PlatformError anError = readReturnError(stream, buffer, count, sizeof(buffer[0]));
-
- if (anError == FW_xNoError)
- {
- for (; count > 0; --count, ++buffer)
- *buffer = swapLong(*buffer);
- }
-
- return anError;
- }
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_PrivReadableStream_ReadFloats
- //----------------------------------------------------------------------------------------
-
- FW_PlatformError SL_API
- FW_PrivReadableStream_ReadFloats(FW_HReadableStream stream,
- float* buffer,
- long count)
- {
- return readReturnError(stream, buffer, count, sizeof(buffer[0]));
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_PrivReadableStream_ReadDoubles
- //----------------------------------------------------------------------------------------
-
- FW_PlatformError SL_API
- FW_PrivReadableStream_ReadDoubles(FW_HReadableStream stream,
- double* buffer,
- long count)
- {
- return readReturnError(stream, buffer, count, sizeof(buffer[0]));
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_PrivWritableStream_Construct
- //----------------------------------------------------------------------------------------
-
- FW_PlatformError SL_API
- FW_PrivWritableStream_Construct(FW_HWritableStream stream,
- FW_OSink* sink,
- FW_OObjectRegistry* objectRegistry,
- int numberFormat)
- {
-
- FW_RETURN_ERR_TRY
- {
- // Make safe for destruction
- stream->fArchiveCreatedObjectRegistry = (objectRegistry == 0);
-
- stream->fSink = sink;
- stream->fObjectRegistry = objectRegistry;
- stream->fReserved = 0;
- FW_PrimitiveSetMemory(&stream->fev, sizeof(stream->fev), 0);
-
- stream->fNumberFormat = numberFormat;
- #if FW_NATIVE_BIGENDIAN
- if (stream->fNumberFormat == FW_kStream_BigEndian)
- stream->fNumberFormat = FW_kStream_NativeEndian;
- #endif
- #if FW_NATIVE_LITTLEENDIAN
- if (stream->fNumberFormat == FW_kStream_LittleEndian)
- stream->fNumberFormat = FW_kStream_NativeEndian;
- #endif
-
- if (stream->fArchiveCreatedObjectRegistry)
- stream->fObjectRegistry = new FW_OBasicObjectRegistry;
- }
- FW_RETURN_ERR_CATCH
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_PrivWritableStream_CopyConstruct
- //----------------------------------------------------------------------------------------
-
- FW_PlatformError SL_API
- FW_PrivWritableStream_CopyConstruct(FW_HWritableStream stream,
- FW_HWritableStream original)
- {
- stream->fSink = original->fSink;
- stream->fObjectRegistry = original->fObjectRegistry;
- stream->fev = original->fev;
- stream->fNumberFormat = original->fNumberFormat;
- stream->fArchiveCreatedObjectRegistry = FALSE;
- return FW_xNoError;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivWritableStream_Destruct
- //----------------------------------------------------------------------------------------
-
- FW_PlatformError SL_API
- FW_PrivWritableStream_Destruct(FW_HWritableStream stream)
- {
- FW_RETURN_ERR_TRY
- {
- if (stream->fArchiveCreatedObjectRegistry)
- delete stream->fObjectRegistry;
- }
- FW_RETURN_ERR_CATCH
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_PrivWritableStream_WriteBytes
- //----------------------------------------------------------------------------------------
-
- FW_PlatformError SL_API
- FW_PrivWritableStream_WriteBytes(FW_HWritableStream stream,
- const void* buffer,
- long count)
- {
- return writeReturnError(stream, buffer, count, 1);
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_PrivWritableStream_WriteChars
- //----------------------------------------------------------------------------------------
-
- FW_PlatformError SL_API
- FW_PrivWritableStream_WriteChars(FW_HWritableStream stream,
- const char* buffer,
- long count)
- {
- return writeReturnError(stream, buffer, count, sizeof(buffer[0]));
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_PrivWritableStream_WriteNullTerminatedString
- //----------------------------------------------------------------------------------------
-
- FW_PlatformError SL_API
- FW_PrivWritableStream_WriteNullTerminatedString(FW_HWritableStream stream,
- const char* nullTerminatedString)
- {
- return writeReturnError(stream,
- nullTerminatedString,
- FW_PrimitiveStringLength(nullTerminatedString) + 1,
- sizeof(nullTerminatedString[0]));
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PrivWritableStream_WriteShorts
- //----------------------------------------------------------------------------------------
-
- FW_PlatformError SL_API
- FW_PrivWritableStream_WriteShorts(FW_HWritableStream stream,
- const short* buffer,
- long count)
- {
- if (stream->fNumberFormat == FW_kStream_NativeEndian)
- {
- return writeReturnError(stream, buffer, count, sizeof(buffer[0]));
- }
- else
- {
- FW_RETURN_ERR_TRY
- {
- for (; count > 0; --count, ++buffer)
- {
- short temp = swapShort(*buffer);
- writableIdentity(stream, &temp, 1, sizeof(temp));
- }
- }
- FW_RETURN_ERR_CATCH
- }
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_PrivWritableStream_WriteLongs
- //----------------------------------------------------------------------------------------
-
- FW_PlatformError SL_API
- FW_PrivWritableStream_WriteLongs(FW_HWritableStream stream,
- const long* buffer,
- long count)
- {
- if (stream->fNumberFormat == FW_kStream_NativeEndian)
- {
- return writeReturnError(stream, buffer, count, sizeof(buffer[0]));
- }
- else
- {
- FW_RETURN_ERR_TRY
- {
- for (; count > 0; --count, ++buffer)
- {
- long temp = swapLong(*buffer);
- writableIdentity(stream, &temp, 1, sizeof(temp));
- }
- }
- FW_RETURN_ERR_CATCH
- }
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_PrivWritableStream_WriteFloats
- //----------------------------------------------------------------------------------------
-
- FW_PlatformError SL_API
- FW_PrivWritableStream_WriteFloats(FW_HWritableStream stream,
- const float* buffer,
- long count)
- {
- return writeReturnError(stream, buffer, count, sizeof(buffer[0]));
- }
-
-
- //----------------------------------------------------------------------------------------
- // FW_PrivWritableStream_WriteDoubles
- //----------------------------------------------------------------------------------------
-
- FW_PlatformError SL_API
- FW_PrivWritableStream_WriteDoubles(FW_HWritableStream stream,
- const double* buffer,
- long count)
- {
- return writeReturnError(stream, buffer, count, sizeof(buffer[0]));
- }
-
-
-